package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.common.Assert;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.exception.BadException;
import com.lwc.cfdns.pojo.bo.system.GroupDataBO;
import com.lwc.cfdns.pojo.dto.system.GroupDataDTO;
import com.lwc.cfdns.pojo.entity.SystemGroup;
import com.lwc.cfdns.pojo.entity.SystemGroupData;
import com.lwc.cfdns.pojo.vo.system.GroupDataVO;
import com.lwc.cfdns.pojo.vo.system.GroupVO;
import com.lwc.cfdns.service.SystemGroupDataService;
import com.lwc.cfdns.mapper.SystemGroupDataMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import com.lwc.cfdns.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_group_data(后台组合数据详情表)】的数据库操作Service实现
 * @createDate 2023-07-12 14:32:11
 */
@Service
public class SystemGroupDataServiceImpl extends ServiceImpl<SystemGroupDataMapper, SystemGroupData> implements SystemGroupDataService {
    @Autowired
    private SystemGroupServiceImpl systemGroupService;

    private static final Logger log = LoggerFactory.getLogger(SystemGroupDataServiceImpl.class);

    /**
     * 预处理组合数据表单
     *
     * @param groupDataDTO
     * @param view
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/13 16:27:28
     */
    private Map<String, Object> preGroupData(GroupDataDTO groupDataDTO, GroupVO view) {
        Map<String, Object> inputData = (Map<String, Object>) groupDataDTO.getGroupValues();
        log.info("组合提交进来的数据：{}", inputData);
        Map<String, Object> data = new HashMap<>();
        for (Map<String, Object> map : (List<Map<String, Object>>) view.getFields()) {
            String field = ConvertUtils.toStr(map.get("field"));
            String value = ConvertUtils.toStr(inputData.get(field), "");
            String name = ConvertUtils.toStr(map.get("name"));
            log.info("字段名{},必填{}", field, map.get("is_require"));
            if (ConvertUtils.toStr(map.get("is_require")).equals("1") && StringUtils.isEmpty(value)) {
                throw new BadException(name + "字段为必填项！");
            }
            data.put(field, value);
        }
        return data;
    }

    /**
     * 创建
     *
     * @param groupDataDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/13 15:53:57
     */
    @Override
    public Map<String, Object> create(GroupDataDTO groupDataDTO) {
        GroupVO view = systemGroupService.view(groupDataDTO.getGroupId());
        System.out.printf(view.toString());
        Assert.notNull(view, ResultCode.NOT_DATA_FOUNT);
        // 预处理组合数据表单
        Map<String, Object> data = preGroupData(groupDataDTO, view);

        SystemGroupData systemGroupData = new SystemGroupData();
        systemGroupData.setGroupId(view.getId());
        systemGroupData.setSort(groupDataDTO.getSort());
        systemGroupData.setGroupValues(data);
        systemGroupData.setStatus(groupDataDTO.getStatus());
        systemGroupData.setRemark(groupDataDTO.getRemark());
        this.baseMapper.insert(systemGroupData);
        Map<String, Object> map = new HashMap<>();
        map.put("id", systemGroupData.getId());
        return map;
    }

    /**
     * 修改
     *
     * @param groupDataDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/13 16:32:00
     */
    @Override
    public Map<String, Object> update(GroupDataDTO groupDataDTO) {
        SystemGroupData systemGroupData = this.baseMapper.selectById(groupDataDTO.getId());
        Assert.notNull(systemGroupData, ResultCode.NOT_DATA_FOUNT);
        GroupVO view = systemGroupService.view(systemGroupData.getGroupId());
        // 预处理组合数据表单
        Map<String, Object> data = preGroupData(groupDataDTO, view);
        systemGroupData.setGroupId(view.getId());
        systemGroupData.setSort(groupDataDTO.getSort());
        systemGroupData.setGroupValues(data);
        systemGroupData.setStatus(groupDataDTO.getStatus());
        systemGroupData.setRemark(groupDataDTO.getRemark());
        int i = this.baseMapper.updateById(systemGroupData);
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @return GroupDataVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/13 16:10:50
     */
    @Override
    public GroupDataVO view(Long id) {
        SystemGroupData systemGroupData = this.baseMapper.selectById(id);
        Assert.notNull(systemGroupData, ResultCode.NOT_DATA_FOUNT);
        GroupDataVO groupDataVO = new GroupDataVO();
        BeanUtils.copyProperties(systemGroupData, groupDataVO);
        groupDataVO.setGroup(systemGroupService.view(systemGroupData.getGroupId()));
        return groupDataVO;
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/13 16:44:12
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i = 0;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemGroupData>().in(SystemGroupData::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 列表
     *
     * @param groupDataBO
     * @return IPage<SystemGroupData>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/13 16:45:04
     */
    @Override
    public IPage<SystemGroupData> lists(GroupDataBO groupDataBO) {
        Page<SystemGroupData> groupPage = this.baseMapper.selectPage(groupDataBO.toPage(), groupDataBO.toQueryWrapper());
        // 组合数据字体
        GroupVO view = systemGroupService.view(groupDataBO.getGroupId());

        // 按OV返回
        groupPage.convert(s -> {
            GroupDataVO groupDataVO = new GroupDataVO();
            groupDataVO.setGroup(view);
            BeanUtils.copyProperties(s, groupDataVO);
            return groupDataVO;
        });
        return groupPage;
    }
}




